<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Agent extends Controller
{
    /**
     * 定时任务,到期代理时间结束
     */
    public function crontabAgent()
    {
        $crontab_data = Db::table('ad_user_agent_crontab')->where('end_time', '<=', time())->where(['deleted_at' => 0])->select();
        if (!empty($crontab_data)) {
            Db::transaction(function () use ($crontab_data) {
                foreach ($crontab_data as $item => $value) {
                    Db::table('ad_user_info')->where(['uid' => $value['uid']])->update(['is_open' => 2]);
                    Db::table('ad_user_agent')->where(['uid' => $value['uid']])->update(['status' => 0]);
                    Db::table('ad_user_agent_crontab')->where(['id' => $value['id']])->update(['deleted_at' => 1]);
                }
            });
        }
        return json('success');
    }


    public function getWithdrawLog(Request $request)
    {
        $uid      = $request->param('uid') ?: 11;
        $all_data = Db::name('finance')->where(['uid' => $uid])->select();

        //分月显示,以月做键名
        foreach ($all_data as $item => $value) {
            $all_data[$item]['create_time_month'] = date('Y-m', $value['create_time']);
        }
        $find_data = [];
        foreach ($all_data as $item => $value) {
            $find_data[$value['create_time_month']][] = $value;
        }

        dd($find_data);
    }
}